package com.tsing.common.utils;

import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileCheckUtil {

    public static boolean checkImageSuffix(MultipartFile imgPath) {
        Boolean flag = false;
        //图片格式
        String[] FILETYPES = new String[]{
                ".jpg", ".jpeg", ".BMP", ".JPG",
                ".png", ".bmp", ".PNG", ".JPEG"
        };
        String originalFilename = imgPath.getOriginalFilename();
        if (!StringUtils.isBlank(originalFilename)) {
            for (int i = 0; i < FILETYPES.length; i++) {
                if (originalFilename.endsWith(FILETYPES[i])) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            throw new BusinessException(CommonException.Proxy.FILE_FORMAT_CHECK_ERREO);
        }
        return flag;
    }

    public static boolean checkWordSuffix(MultipartFile file) {
        Boolean flag = false;
        String[] FILETYPES = new String[]{
                ".DOCX", ".docx",
                ".DOC", ".doc",
                ".PDF", ".pdf",
                ".xlsx", ".xls",
                ".XLSX", ".XLS"
        };
        String originalFilename = file.getOriginalFilename();
        if (!StringUtils.isBlank(originalFilename)) {
            for (int i = 0; i < FILETYPES.length; i++) {
                if (originalFilename.endsWith(FILETYPES[i])) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            throw new BusinessException(CommonException.Proxy.FILE_FORMAT_CHECK_ERREO);
        }
        return flag;
    }

    public static boolean checkVideoSuffix(MultipartFile file) {
        Boolean flag = false;
        String[] FILETYPES = new String[]{
                ".mp4", ".MP4",
                ".rmvb", ".RMVB",
                ".m4v", ".M4V",
                ".wmv", ".WMV",
                ".avi", ".AVI",
                ".mpg", ".mpg",
                ".mov", ".MOV",
                ".ram", ".RAM",
                ".rm", ".RM",
        };
        String originalFilename = file.getOriginalFilename();
        if (!StringUtils.isBlank(originalFilename)) {
            for (int i = 0; i < FILETYPES.length; i++) {
                if (originalFilename.endsWith(FILETYPES[i])) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            throw new BusinessException(CommonException.Proxy.FILE_FORMAT_CHECK_ERREO);
        }
        return flag;
    }

}
